<?php

namespace App\Admin\Controllers\Mp;

use App\Admin\Actions\Tree\SyncToWeWeiXinMp;
use App\Models\Mp\WxMpCustomMenu;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Tree;
use Illuminate\Database\Eloquent\Model;

class WxMpCustomMenuController extends AdminController
{
    public function index(Content $content)
    {
        return $content->header('公众号自定义菜单')->description('公众号菜单本身有缓存，你可以重新关注以刷新菜单的缓存，<b style="color:red;">先保存排序，再点同步</b>')
            ->body(function (Row $row) {
                $tree = new Tree(new WxMpCustomMenu());
                $tree->expand();
                $tree->maxDepth(3);
//                $tree->disableCreateButton();
                $row->column(12, $tree);
                $tree->tools(function (Tree\Tools $tools) {
                    $tools->add(new SyncToWeWeiXinMp());
                });
            });
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxMpCustomMenu(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent_id');
            $grid->column('order');
            $grid->column('type');
            $grid->column('name');
            $grid->column('key');
            $grid->column('url');
            $grid->column('media_id');
            $grid->column('appid');
            $grid->column('pagepath');
            $grid->column('article_id');
            $grid->column('depth');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxMpCustomMenu(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('order');
            $show->field('type');
            $show->field('name');
            $show->field('key');
            $show->field('url');
            $show->field('media_id');
            $show->field('appid');
            $show->field('pagepath');
            $show->field('article_id');
            $show->field('depth');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxMpCustomMenu(), function (Form $form) {
            $form->display('id');
            $form->text('parent_id')->default(0)->display(false);
            $form->text('order')->default(0)->display(false);
            $form->text('name')->required();
            $form->radio('type')->options([
                'view' => '跳转URL',
                'scancode_push' => '扫一扫',
                'scancode_waitmsg' => '扫一扫(先询问)',
                'pic_sysphoto' => '相机',
                'pic_photo_or_album' => '拍照相册',
                'pic_weixin' => '微信相册',
                'location_select' => '地理位置选择',
                'media_id' => '图片、音频、视频',
                'article_id' => '图文卡片',
                'article_view_limited' => '跳转到图文',
                'miniprogram' => '打开小程序',
                'click' => '事件推送'
            ])->when(['click', 'scancode_push', 'scancode_waitmsg', 'pic_sysphoto', 'pic_weixin', 'pic_photo_or_album', 'location_select'], function (Form $form){
                $form->text('key')->rules('required_if:type,click,scancode_push,scancode_waitmsg,pic_sysphoto,pic_weixin,pic_photo_or_album,location_select') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
            })->when(['media_id'], function (Form $form){
                $form->text('media_id')->rules('required_if:type,media_id') // 使用required_if
                ->setLabelClass(['asterisk'])->help('<a href="https://img.mini.minisns.cn/images/media_id.png" target="_blank">Media_Id获取教程</a>'); // 显示 * 号;
            })->when(['article_id', 'article_view_limited'], function (Form $form){
                $form->text('article_id')->rules('required_if:type,article_id')->help('<a href="https://img.mini.minisns.cn/images/article_id.png" target="_blank">Article_Id获取教程</a>')
                ->setLabelClass(['asterisk']); // 显示 * 号;
            })->when('miniprogram', function (Form $form){

                $form->text('appid')->default(Settings::get('app_id', ''))->rules('required_if:type,miniprogram') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;;
                $form->text('pagepath')->rules('required_if:type,miniprogram') // 使用required_if
                ->setLabelClass(['asterisk']);
            })->when(['view', 'miniprogram'], function (Form $form){
                $form->text('url')->rules('required_if:type,view,miniprogram') // 使用required_if
                ->setLabelClass(['asterisk']);
            })->default('view');

            $form->text('depth')->default(1)->display(false);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
